package org.irri.smta.service.impl;

import javax.inject.Named;

import org.irri.smta.domain.SmtaReport;
import org.irri.smta.report.ObjectFactory;
import org.irri.smta.report.Smta;
import org.irri.smta.report.Smta.Annex1.Material;
import org.irri.smta.report.Smta.Document;
import org.irri.smta.report.Smta.Provider;
import org.irri.smta.report.Smta.Recipient;
import org.irri.smta.report.SmtaBuilder;
import org.irri.smta.service.ISmtaTransformerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

@Named("smtaTransformerService")
@Transactional(readOnly = true)
public class SmtaTransformerService implements ISmtaTransformerService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(SmtaTransformerService.class);

	@Override
	public Smta transformSmtaReportToSmta(SmtaReport smtaReport) {
		LOGGER.debug("Smta transformSmtaReportToSmta(SmtaReport smtaReport): " + smtaReport);
		try {
			if (smtaReport == null) {
				return null;
			}

			ObjectFactory smtaFactory = new ObjectFactory();

			SmtaBuilder builder = new SmtaBuilder();
			Smta smta = builder.createSmtaShell();

			smta.setDate(smtaReport.getSmtaDate());
			smta.setLanguage(smtaReport.getLanguage());
			smta.setSymbol(smtaReport.getSymbol());
			smta.setType(smtaReport.getSmtaType());
			smta.setShipName(smtaReport.getShipmentName());

			Provider provider = smta.getProvider();
			provider.setAddress(smtaReport.getProviderAddress());
			provider.setCountry(smtaReport.getProviderCountry());
			provider.setEmail(smtaReport.getProviderEmail());
			provider.setName(smtaReport.getProviderName());
			provider.setPid(smtaReport.getProviderPid());
			provider.setType(smtaReport.getProviderType());

			Recipient recipient = smta.getRecipient();
			recipient.setAddress(smtaReport.getRecipientAddress());
			recipient.setCountry(smtaReport.getRecipientCountry());
			recipient.setName(smtaReport.getRecipientName());
			recipient.setPid(smtaReport.getRecipientPid());
			recipient.setType(smtaReport.getRecipientType());

			for (org.irri.smta.domain.Material mat : smtaReport.getMaterials()) {
				Material material = smtaFactory.createSmtaAnnex1Material();
				material.setAncestry(mat.getAncestry());
				material.setCrop(mat.getCrop());
				material.setPud(mat.getPud());
				material.setSampleID(mat.getSampleId());
				// Add material
				smta.getAnnex1().getMaterial().add(material);
			}

			Document document = smta.getDocument();
			document.setLocation(smtaReport.getDocumentLocation());
			document.setRetInfo(smtaReport.getDocumentRetrievalInfo());

			return smta;
		} catch (Exception e) {
			LOGGER.error(e.getMessage());
		}
		return null;
	}

	@Override
	public String transformSmtaReportToXml(Smta smta) throws Exception {
		LOGGER.debug("String transformSmtaReportToXml(Smta smta): " + smta);
		SmtaBuilder builder = new SmtaBuilder();
		String xml = builder.createXml(smta, "UTF-8");
		return xml;
	}

}
